﻿<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TB4" xmlns:Converters="clr-namespace:WPF.Common.Converters;assembly=WPF.Common">

    <Converters:VisibilityConverter x:Key="VisibilityConverter" />
    <Style x:Key="ToolbarItemContainerStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Visibility" Value="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected, Converter={StaticResource VisibilityConverter}}"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
    </Style>
    <Converters:IsGreaterThanConverter x:Key="GreaterThanConverter" />
    <Style x:Key="Style1" TargetType="{x:Type GroupItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <ListBox 
    						Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:GroupToolbar}, Mode=FindAncestor}}"
    						ItemsSource="{Binding Items}" 
    						ItemTemplate="{Binding ToolbarItemTemplate, RelativeSource={RelativeSource AncestorType={x:Type local:GroupToolbar}, Mode=FindAncestor}}"
    						ItemContainerStyle="{StaticResource ToolbarItemContainerStyle}"
    						ContextMenuService.Placement="Right" ContextMenuService.IsEnabled="{Binding Items.Count, Converter={StaticResource GreaterThanConverter}, ConverterParameter=1}">
                        <ListBox.ContextMenu>
                            <local:SelectorContextMenu 
    								ItemsSource="{Binding PlacementTarget.Items, RelativeSource={RelativeSource Self}}"
    								ItemContainerStyle="{Binding PlacementTarget.Tag.SelectorContextMenuItemContainerStyle, RelativeSource={RelativeSource Self}}"
    								ItemTemplate="{Binding PlacementTarget.Tag.SelectorContextMenuItemTemplate, RelativeSource={RelativeSource Self}}" />
                        </ListBox.ContextMenu>
                        <ListBox.Template>
                            <ControlTemplate>
                                <Grid>
                                    <ItemsPresenter />
                                    <Viewbox Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(ContextMenuService.IsEnabled), Converter={StaticResource VisibilityConverter}}" VerticalAlignment="Bottom" HorizontalAlignment="Right" MaxHeight="4" MaxWidth="4" Stretch="Uniform" Margin="0,0,1,1" >
                                    	<Path Data="M103.37506,32.875 L122.688,15.666622 122.5625,32.875 z"  Stretch="Fill" >
                                    		<Path.Fill>
                                    			<SolidColorBrush Color="{DynamicResource {x:Static SystemColors.ControlDarkColorKey}}"/>
                                    		</Path.Fill>
                                    	</Path>
                                    </Viewbox>
                                </Grid>
                            </ControlTemplate>
                        </ListBox.Template>
                    </ListBox>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="{x:Type local:GroupToolbar}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:GroupToolbar}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <ListBox x:Name="PART_RootSelector" ItemsSource="{TemplateBinding ItemsSource}">
                            <ListBox.GroupStyle>
                                <GroupStyle ContainerStyle="{StaticResource Style1}"/>
                            </ListBox.GroupStyle>
                        </ListBox>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

    </Style>
</ResourceDictionary>
